Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

499
Visualizações
"Unhandled Error" message while setting up server through http module in node js

I have been writing the following code to set up a basic server through node.js. My code -

    const http  = require('http')
    const server = http.createServer((req, res) => {
    // console.log(req)
      if(req.url === '/'){
          res.end("Welcome to our home page")
      }
      if(req.url === '/aboutus'){
          res.end("Welcome to about us page")
      }
      res.end(`
          <h1> OOPS !!! </h1>
          <p>You have requested for any wrong address </p>
          <a href = "/">Get back to HOME Page</a>
     `)
    })
    server.listen(5000)

But this code while being run is giving the following error in my console. Error --

node:events:368
      throw er; // Unhandled 'error' event
      ^

Error [ERR_STREAM_WRITE_AFTER_END]: write after end
    at new NodeError (node:internal/errors:371:5)
    at ServerResponse.end (node:_http_outgoing:846:15)
    at Server.<anonymous> (/home/tirtharaj/MERN/Node/tutorial(fcc)/8-Built-in Modules/6-httpModule(brief).js:10:9)
    at Server.emit (node:events:390:28)
    at parserOnIncoming (node:_http_server:951:12)
    at HTTPParser.parserOnHeadersComplete (node:_http_common:128:17)
Emitted 'error' event on ServerResponse instance at:
    at emitErrorNt (node:_http_outgoing:726:9)
    at processTicksAndRejections (node:internal/process/task_queues:84:21) {
  code: 'ERR_STREAM_WRITE_AFTER_END'
}

I am not able to find any error in my code. Any refresh in the web browser in giving error. So please help me run this code.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

This will solve your problem, you need to have all the code wrapped in if else statements so that it can always terminate successfully with only one possible path through the code.

const http = require("http");
const server = http.createServer((req, res) => {
  if (req.url === "/") {
    res.end("Welcome to our home page");
  } else if (req.url === "/aboutus") {
    res.end("Welcome to about us page");
  } else {
    res.end(`
        <h1> OOPS !!! </h1>
        <p>You have requested for any wrong address </p>
        <a href = "/">Get back to HOME Page</a>
    `);
  }
});
server.listen(5000);

Personally, I would use a switch case statement for this to keep it nice and clean.

const http = require("http");
const server = http.createServer((req, res) => {
  switch (req.url) {
    case "/":
      res.end("Welcome to our home page");
      break;

    case "/aboutus":
      res.end("Welcome to about us page");
      break;

    default:
      res.end(`
        <h1> OOPS !!! </h1>
        <p>You have requested for any wrong address </p>
        <a href = "/">Get back to HOME Page</a>
      `);
      break;
  }
});
server.listen(5000);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda